#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : micah
# @time    :  
# @function: 
# @version :


import pymysql
from datetime import datetime


def insert_data():
    db = pymysql.connect(host='localhost', port=3305, user='root',password='123456', db='python_test_1')
    cursor = db.cursor()

    sql = """
    insert into employee(first_name, last_name, age, sex, income, create_time) values
    ('%s', '%s', '%d', '%s', '%d', '%s')
    """ % ('安', '娜', 18, '女', 17877, datetime.now())

    try:
        cursor.execute(sql)
        # pymysql在进行数据更新、数据删除的时候会自动触发事务
        db.commit()
        print('数据插入成功...')
    except Exception as e:
        print('数据插入成功...')
        db.rollback()
    finally:
        cursor.close()
        db.close()


insert_data()
